// pages/userList/userList.js
var util = require('../../utils/util');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userList:[],
    isHasNew:false,
    content_id: '',
    user_id: '',
    department: [
      {
          id: '',
          content_name: '请选择部门'
      }
  ],
  dIndex: 0,
  uname: ''
  },
  toNew(){
      wx.navigateTo({
          url:'/pages/checkNew/checkNew'
      })
  },
  bindDepartmentChange(e) {
    console.log(e)
    let dIndex = e.detail.value;
    let department_id = this.data.department[dIndex].id;
    this.setData({
      content_id: department_id,
      dIndex: dIndex
    })
    this.getList()
  },
  // 获取部门
  getDepartment() {
    let that = this;
    util.getAjax(`${app.globalData.apiUrl}/Api/Food/get_food_content`,res => {
      const department = that.data.department.concat(res.data)
      // console.log(department)
      that.setData({
        department: department
      })
      // console.log(that.data.department)
    },{
        status: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      util.setNavBarTitle('员工列表');
      this.setData({
        slideButtons: [{
          text: '修改',
          data: {type: 1}
        },{
          type: 'warn',
          text: '删除',
          data: {type: 2}
        }]
    });
  },
  slideButtonTap(e) {
    let that = this;
    console.log('slide button tap', e.detail, e.currentTarget)
    if (e.detail.data.type === 1) {
      // 修改员工信息
      wx.navigateTo({
        url:'/pages/userInfo/userInfo?userId=' + e.currentTarget.dataset.id  
      })
    } else if(e.detail.data.type === 2) {
      // 删除员工
      let delId = e.currentTarget.dataset.id
      wx.showModal({
        title:'提示',
        content:'是否确认删除员工 ' + e.currentTarget.dataset.uname + ' ?',
        success(res){
            if(res.confirm){
                util.getAjax(`${app.globalData.apiUrl}/Api/Food/save_user`,res=>{
                    if(res.data == 1){
                        util.showToast('员工删除成功','success',1000);
                        that.getList();
                    }
                },{
                    id:delId,
                    status:2
                })
            }
        }
    })
    }
  },
  hasNew(){
      var _this = this;
      util.getAjax(`${app.globalData.apiUrl}/Api/Food/get_user`,res=>{
          console.log(res.data)
          if(res.data.length!=0){
              console.log(2);
              _this.setData({
                  isHasNew:true
              })
          }else{
              _this.setData({
                  isHasNew:false
              })
          }
      },{
          status:4
      })
  },
  nameChange(e){
    const that = this;
    that.setData({
      uname: e.detail.value
    })
    
  },
  searchName() {
    const that = this;
    if (that.data.uname) {
      util.getAjax(`${app.globalData.apiUrl}/Api/Food/get_user_id`,res => {
        console.log(res.data)
        if(res.data) {
          that.data.user_id = res.data.id
          that.getList()
        } else {
          util.showToast('系统不存在该员工！','none', 2000)
        }
      },{name: that.data.uname })
    } else {
      that.data.user_id = '';
      that.getList()
    }
  },
  //status 0 未通过 1 通过
  getList(){
    var _this = this;
    util.getAjax(`${app.globalData.apiUrl}/Api/Food/get_user`,res=>{
        // console.log(res)
        console.log(res.data)
        _this.setData({
            userList:res.data,
            howManch:res.data.length
        })
    },{
        status:1,
        content_id: _this.data.content_id,
        name: _this.data.uname
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.getList();
      this.hasNew();
      this.getDepartment()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
